<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <title>登录</title>
    <link rel="stylesheet" type="text/css" href="../css/api.css" />
    <style>
        html,
        body {
            font-size: 14px;
            height: 100%;
            background-color: #f2f2f2;
        }

        header {
            position: fixed;
            top: 0;
            width: 100%;
            height: 75px;
            color: #fff;
            text-align: left;
            background-color: #FBB40E;
        }

        header .back {
            display: flex;
            justify-content: space-between;
            margin-top: 38px;
            width: 100%;
            height: 1.5rem;
            text-align: center;
        }

        .back img {
            margin-left: 1rem;
            width: 1.5rem;
            height: 1.5rem;
            vertical-align: middle;
        }

        .back span:nth-child(3) {
            margin-right: 1rem;
        }

        main {
            padding-top: 75px;
        }

        main .myhead {
            height: 20rem;
            text-align: center;
            padding-top: 5rem;
            box-sizing: border-box;
            background-color: #FBB40E;
        }

        .myhead .head {
            width: 7rem;
            height: 7rem;
            margin: auto;
            background-color: #fff;
            border-radius: 50%;
            overflow: hidden;
        }

        .myhead .head img {
            width: 100%;
            height: 100%;
        }

        .myhead span {
            color: #fff;
            font-weight: bold;
            font-size: 1.5rem;
            margin-top: 1rem;
        }

        main .loginbox {
            width: 90%;
            padding: 1rem 0.5rem;
            margin: 1rem auto 0;
            box-sizing: border-box;
            background-color: #fff;
            border-radius: 0.5rem;
            box-shadow: 0 0 0.1rem #FBB40E;
        }

        .loginbox-t {
            text-align: center;
        }

        .loginTable {
            padding: 1rem;
            box-sizing: border-box;
        }

        .active {
            color: #2E7AFA;
        }

        .loginTable li {
            padding: 0.5rem 0;
            height: 2rem;
            border-bottom: 1px solid #f2f2f2;
        }

        li.password {
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        li.password .showpwd {
            vertical-align: middle;
            width: 1.5rem;
            height: 1.5rem;
            display: none;
        }

        .loginBtn {
            margin-top: 1rem;
            width: 100%;
            height: 3rem;
            color: #fff;
            font-size: 1.2rem;
            border-radius: 0.5rem;
            background-color: #FBB40E;
        }
    </style>
</head>

<body>
    <header>
        <div class="back">
            <img src="../image/icon/back.png" alt="" onclick="api.closeWin()">
            <span>登录</span>
            <span onclick="gotoReg()">注册</span>
        </div>
    </header>
    <main>
        <div class="myhead">
            <div class="head">
                <img src="../image/icon/head.png" alt="">
            </div>
        </div>
        <div class="loginbox">
            <div class="loginbox-t">
                <div class="login active">登录</div>
            </div>
            <ul class="loginTable">
                <li>
                    <input type="text" name="" value="" placeholder="请输入用户名" id="uname">
                </li>
                <li class="password">
                    <input type="password" name="" value="" placeholder="请输入密码" id="password">
                    <img src="../image/icon/hidepwd.png" alt="" class="showpwd">
                </li>
                <li>
                    <button class="loginBtn">登录</button>
                </li>
            </ul>
        </div>
    </main>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.11.3.min.js"></script>
<script type="text/javascript">
    apiready = function() {

    };

    function gotoReg() {
        api.openWin({
            name: 'register',
            url: './register.html',
            slidBackEnabled: false
        });
    }

    $('.loginBtn').click(function() {
      var uname = $('#uname').val();
      var pwd = $('#password').val();
        if (!uname) {
          api.toast({
              msg: '用户名不能为空',
              duration: 2000,
              location: 'bottom'
          });
          return
        };
        if (!pwd) {
          api.toast({
              msg: '密码不能为空',
              duration: 2000,
              location: 'bottom'
          });
          return
        };
        api.ajax({
            url: 'http://122.112.138.133/wangt/jianzhi/queryUser.com',
            method: 'post',
            data: {
                body: {
                  "data": {
                    "username": uname,
                    "password": pwd
                  }
                }
            }
        },function(ret, err){
            if (ret) {
                if(ret.data.total == 1){
                  $api.setStorage('username', ret.data.records[0].username);
                  $api.setStorage('userid', ret.data.records[0].id);
                  $('#uname').val('');
                  $('#password').val('');
                  api.closeWin({
                      name: 'main'
                  });
                  api.openWin({
                      name: 'main',
                      url: './main.html',
                      pageParam: {},
                      slidBackEnabled: false
                  });
                } else {
                  api.toast({
                      msg: '用户名或密码错误',
                      duration: 2000,
                      location: 'bottom'
                  })
                }
            }
        });
    })

    $('.loginTable .password>input').bind('input propertychange', function() {
        if ($(this).val() != '' && $.trim($(this).val()).length != 0) {
            $('.showpwd').css('display', "block");
            $('.showpwd').click(function() {
                if ($(this).attr('src') == "../image/icon/hidepwd.png") {
                    $(this).attr('src', "../image/icon/showpwd.png");
                    $('.password input').attr('type', "text");
                } else {
                    $(this).attr('src', "../image/icon/hidepwd.png");
                    $('.password input').attr('type', "password");
                }
            })
        } else {
            $('.showpwd').css('display', "none");
        }
    })
</script>

</html>
